﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;
using System.ServiceModel;

namespace TestTransfert
{
    /*
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IRemoteStorage" maxReceivedMessageSize="2147483647"></binding>
        <binding name="HttpEndPoint" messageEncoding="Mtom" />
      </basicHttpBinding>
      <netTcpBinding>
        <binding name="TransfertTCPEndPoint" receiveTimeout="10:01:00" sendTimeout="10:01:00"
          transferMode="Streamed" maxBufferPoolSize="66560" maxBufferSize="66560" maxConnections="66560"
          maxReceivedMessageSize="2147483647" portSharingEnabled="true">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="66560" />
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8001/RemoteStorage/RemoteStorage.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRemoteStorage"
                contract="IRemoteStorage" name="BasicHttpBinding_IRemoteStorage" />
      <endpoint address="http://localhost:8001/RemoteStorage/TransferService.svc"
                binding="basicHttpBinding" bindingConfiguration="HttpEndPoint"
                contract="ITransferService" name="HttpEndPoint" />
      <endpoint address="net.tcp://vmdev004dt.Storage-bpo.com:5000/RemoteStorage/TransferService.svc"
          binding="netTcpBinding" bindingConfiguration="TransfertTCPEndPoint"
          contract="ITransferService" name="TransfertTCPEndPoint">
        <identity>
          <servicePrincipalName value="host/VMDEV004DT.Storage-bpo.com" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
    /// </summary>*/
    public enum BindingType
    {
        TCP = 0,
        http = 1
    }
    public class BindingHelper
    {
        public string BaseAdress { get; set; }
        public int BufferReadSize { get; set; }
        public int BufferWriteSize { get; set; }
        public BindingHelper(string baseAdress)
        {
            this.BaseAdress = baseAdress;
        }
        public Binding GetTransfertBinding(BindingType type)
        {
            Binding result = null;
            switch (type)
            {
                case BindingType.TCP:
                    NetTcpBinding binding = new NetTcpBinding();
                    binding.ReceiveTimeout = new TimeSpan(10, 1, 0);
                    binding.SendTimeout = new TimeSpan(10, 1, 0);
                    binding.TransferMode = TransferMode.Streamed;
                    binding.MaxBufferPoolSize = 66560;
                    binding.MaxBufferSize = 66560;
                    binding.MaxConnections = 66560;
                    binding.MaxReceivedMessageSize = 2147483647;
                    binding.PortSharingEnabled = true;

                    binding.ReaderQuotas.MaxDepth = 32;
                    binding.ReaderQuotas.MaxStringContentLength = 2147483647;
                    binding.ReaderQuotas.MaxArrayLength = 2147483647;
                    binding.ReaderQuotas.MaxBytesPerRead = 66560;
                    result = binding;
                    break;
                case BindingType.http:
                    BasicHttpBinding bindingHTTP = new BasicHttpBinding();
                    bindingHTTP.ReceiveTimeout = new TimeSpan(10, 1, 0);
                    bindingHTTP.SendTimeout = new TimeSpan(10, 1, 0);
                    bindingHTTP.TransferMode = TransferMode.Streamed;
                    bindingHTTP.MaxBufferPoolSize = 66560;
                    bindingHTTP.MaxBufferSize = 66560;
                    bindingHTTP.MessageEncoding = WSMessageEncoding.Mtom;
                    bindingHTTP.MaxReceivedMessageSize = 2147483647;

                    bindingHTTP.ReaderQuotas.MaxDepth = 32;
                    bindingHTTP.ReaderQuotas.MaxStringContentLength = 2147483647;
                    bindingHTTP.ReaderQuotas.MaxArrayLength = 2147483647;
                    bindingHTTP.ReaderQuotas.MaxBytesPerRead = 66560;
                    result = bindingHTTP;
                    break;
                default:
                    result = null;
                    break;
            }
            return result;
        }

        public Binding GetDiscoverBinding(BindingType type)
        {
            Binding result = null;
            switch (type)
            {
                case BindingType.TCP:
                    NetTcpBinding binding = new NetTcpBinding();
                    binding.ReceiveTimeout = new TimeSpan(10, 1, 0);
                    binding.SendTimeout = new TimeSpan(10, 1, 0);
                    binding.TransferMode = TransferMode.Buffered;
                    binding.MaxBufferPoolSize = 66560;
                    binding.MaxBufferSize = 66560;
                    binding.MaxConnections = 66560;
                    binding.MaxReceivedMessageSize = 2147483647;
                    binding.PortSharingEnabled = true;

                    binding.ReaderQuotas.MaxDepth = 32;
                    binding.ReaderQuotas.MaxStringContentLength = 2147483647;
                    binding.ReaderQuotas.MaxArrayLength = 2147483647;
                    binding.ReaderQuotas.MaxBytesPerRead = 66560;
                    result = binding;
                    break;
                case BindingType.http:
                    BasicHttpBinding bindingHTTP = new BasicHttpBinding();
                    bindingHTTP.ReceiveTimeout = new TimeSpan(10, 1, 0);
                    bindingHTTP.SendTimeout = new TimeSpan(10, 1, 0);
                    bindingHTTP.TransferMode = TransferMode.Buffered;
                    bindingHTTP.MaxBufferPoolSize = 66560;
                    bindingHTTP.MaxBufferSize = 66560;
                    bindingHTTP.MessageEncoding = WSMessageEncoding.Text;
                    bindingHTTP.MaxReceivedMessageSize = 2147483647;

                    bindingHTTP.ReaderQuotas.MaxDepth = 32;
                    bindingHTTP.ReaderQuotas.MaxStringContentLength = 2147483647;
                    bindingHTTP.ReaderQuotas.MaxArrayLength = 2147483647;
                    bindingHTTP.ReaderQuotas.MaxBytesPerRead = 66560;
                    result = bindingHTTP;
                    break;
                default:
                    result = null;
                    break;
            }
            return result;
        }

        public EndpointAddress GetTransferEndpoint(string BaseAdress, BindingType type)
        {
            this.BaseAdress = BaseAdress;
            return null;
            //EndpointAddress adress = new EndpointAddress();
        }
    }
}
